<?php

namespace App\Admin\Controllers;

use App\Models\Record;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class RecordController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '出入记录';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Record());
        $grid->model()->orderBy('id', 'desc');
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1/2, function ($filter) {
                $filter->equal('apply_id', '申请编号');
                $filter->equal('type', '出入类型')->select(Record::$_type);
            });
            $filter->column(1/2, function ($filter) {
                $filter->between('created_at', '创建时间')->datetime();
            });
        });
        $grid->column('id', __('编号'));
        $grid->column('apply_id', __('申请编号'));
        $grid->column('type', __('出入类型'))->using(Record::$_type);
        $grid->column('created_at', __('创建时间'))->display(function($time) {
            return date('Y-m-d H:i:s', strtotime($time));
        });
        $grid->column('updated_at', __('修改时间'))->display(function($time) {
            return date('Y-m-d H:i:s', strtotime($time));
        });
        $grid->disableCreateButton();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableEdit();
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Record::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('apply_id', __('Apply id'));
        $show->field('type', __('Type'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Record());

        $form->number('apply_id', __('Apply id'));
        $form->switch('type', __('Type'))->default(1);

        return $form;
    }
}
